package cn.novate.jetpackdemo.workmanager;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.work.Data;
import androidx.work.Worker;
import androidx.work.WorkerParameters;

import java.util.HashMap;
import java.util.Map;

public class WorkMain extends Worker {

    private Context mContext;
    private WorkerParameters workerParameters;

    private final static String TAG = WorkMain.class.getCanonicalName();
    public WorkMain(@NonNull Context context, @NonNull WorkerParameters workerParams) {
        super(context, workerParams);
        this.mContext = context;
        this.workerParameters = workerParams;
    }

    @SuppressLint("RestrictedApi")
    @NonNull
    @Override
    public Result doWork() {


        try {
            Thread.sleep(6000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }finally {
            Log.e(TAG,"doWork：  开始执行了。。。。。");
        }

        String mainData = workerParameters.getInputData().getString("dataMian");
        Log.e(TAG,"doWork：  接收到调用的类传递过来的参数了 。。。。。" + mainData);

        Map map = new HashMap();
        map.put("dataMian2", "把我回传去给调用的类");
        @SuppressLint("RestrictedApi")
        Data data = new Data(map);
        //  把数据回传给调用者
        Result.Success success = new Result.Success(data);
        return success;
//        return Result.success();//  本次任务成功
    }
}
